package com.estun.backroundManagementService.modules.device.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.estun.backroundManagementService.modules.device.mapper.DeviceStatusMapper;
import com.estun.backroundManagementService.modules.device.model.DeviceStatus;
import com.estun.backroundManagementService.modules.device.service.DeviceStatusService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Slf4j
@Service
@AllArgsConstructor
public class DeviceStatusServiceImpl extends ServiceImpl<DeviceStatusMapper, DeviceStatus> implements DeviceStatusService {


    @Override
    public Map<String, String> getDeviceStatus() {
        List<DeviceStatus> list = list();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        Map<String, List<DeviceStatus>> statusMap = list.stream()
                .collect(Collectors.groupingBy(DeviceStatus::getStationId));

        Map<String, String> map = new HashMap<>();
        statusMap.forEach((stationId, statuses) -> {
            // 确定当前 station_id 的整体状态
            boolean isRunning = statuses.stream()
                    .anyMatch(status -> "系统运行".equals(status.getAlarmName()) && status.getStatus() == 1);
            boolean isFault = statuses.stream()
                    .anyMatch(status -> "系统故障".equals(status.getAlarmName()) && status.getStatus() == 1);
            boolean isMaintenance = statuses.stream()
                    .anyMatch(status -> "维护模式".equals(status.getAlarmName()) && status.getStatus() == 1);

            // 基于条件确定整体状态
            if (isRunning) {
                map.put(stationId, "系统运行");
            } else if (isFault && !isMaintenance) {
                map.put(stationId, "系统故障");
            } else if (isMaintenance) {
                map.put(stationId, "系统维护");
            }
        });
        return map;
    }

    public String getStatus() {

        return "";
    }


}
